package com.hongshu.file.service.impl;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import com.hongshu.common.core.domain.R;
import com.hongshu.common.core.enums.OssProviderEnum;
import com.hongshu.file.domain.OssConfigDTO;
import com.hongshu.file.service.ISysFileService;
import com.hongshu.file.utils.FileUploadUtils;
import com.hongshu.system.api.RemoteWebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Map;

@Service
public class AliyunSysFileServiceImpl implements ISysFileService {

    @Autowired
    private RemoteWebService remoteWebService;


    @Override
    public String uploadFile(MultipartFile file) {
        R<?> r = remoteWebService.getOssConfig();
        if (r == null || r.getData() == null) {
            throw new IllegalStateException("未获取到 OSS 配置：remoteWebService.getOssConfig() 返回为空");
        }
        Map<String, OssConfigDTO> ossMap = JSONUtil.toBean(
                JSONUtil.parseObj(r.getData()),
                new TypeReference<Map<String, OssConfigDTO>>() {
                }, false);
        OssConfigDTO ossConfig = ossMap.get(OssProviderEnum.ALIYUN.getCode());
        if (ossConfig == null) {
            throw new IllegalStateException("未找到阿里云 OSS 配置，key=" + OssProviderEnum.ALIYUN.getCode());
        }
        if (StrUtil.hasBlank(
                ossConfig.getEndpoint(), ossConfig.getAccessKeyId(),
                ossConfig.getAccessKeySecret(), ossConfig.getBucketName())) {
            throw new IllegalStateException("阿里云 OSS 配置不完整，请检查 endpoint/accessKeyId/accessKeySecret/bucketName");
        }

        OSS client = new OSSClientBuilder().build(
                ossConfig.getEndpoint(),
                ossConfig.getAccessKeyId(),
                ossConfig.getAccessKeySecret()
        );
        try {
            String key = FileUploadUtils.extractFilename(file);
//            if (StringUtils.isNotEmpty(ossConfig.getPathPrefix())) {
//                key = ossConfig.getPathPrefix().replaceAll("/+$", "") + "/" + key;
//            }
            ObjectMetadata meta = new ObjectMetadata();
            meta.setContentLength(file.getSize());
            meta.setContentType(file.getContentType());
            client.putObject(ossConfig.getBucketName(), key, file.getInputStream(), meta);
            String base = StrUtil.blankToDefault(
                    ossConfig.getDomain(), "https://" + ossConfig.getBucketName() + "." + ossConfig.getEndpoint());
            return base.replaceAll("/+$", "") + "/" + key;
        } catch (Exception e) {
            throw new RuntimeException("Aliyun OSS upload failed", e);
        } finally {
            client.shutdown();
        }
    }

    @Override
    public String uploadFile(File file) {
        return null;
    }
}
